package com.leyou.auth.config;

import com.leyou.auth.interceptors.LoginIntereceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 实现springboot提供的HandlerInterceptor只需要三步:
 * 1, 实现HandlerInterceptor接口, 编写自己的拦截器
 * 2, 编写一个类继承WebMvcConfigurerAdapter, 并重写addInterceptors方法
 * 3, 实例化我们自定义的拦截器，然后将对像手动添加到拦截器链中（在addInterceptors方法中添加）
 */

public class MvcConfiguration implements WebMvcConfigurer {

    private JwtUtils jwtUtils;

    private ClientProperties properties;


    public MvcConfiguration(@Lazy JwtUtils jwtUtils, ClientProperties properties) {
        this.jwtUtils = jwtUtils;
        this.properties = properties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration registration = registry.addInterceptor(new LoginIntereceptor(jwtUtils));
        if(!CollectionUtils.isEmpty(properties.getIncludeFilterPaths())){
            registration.addPathPatterns(properties.getIncludeFilterPaths());
        }
        // 判断用户是否配置了放行路径，如果没配置就是空
        if(!CollectionUtils.isEmpty(properties.getExcludeFilterPaths())){
            registration.excludePathPatterns(properties.getExcludeFilterPaths());
        }
    }
}
